import Vue from 'vue'
import Router from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import Employee from '../components/employee/Employees.vue'
import Goods from '../components/goods/Goods'
import SpecialGoods from '../components/SpecialGoods'
import DepCateGoods from '../components/DepCateGoods'
import Discounts from '../components/discount/Discounts'
import Header from '../components/Header'
import GoodsList from '../components/GoodsList'
import SubLogin from '../components/SubLogin'
import Registry from '../components/Registry'
import FindPwd from '../components/FindPwd'
import RegistrySuccess from '../components/RegistrySuccess'
import FindPwdSuccess from '../components/FindPwdSuccess'
import MyAccount from '../components/MyAccount'
import DiscountsList from '../components/DiscountsList'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home,
      redirect: '/employees',
      children: [
        {
          // 路径别忘了加斜杠 如果不加斜杠则定位到welcome/users路径上
          path: '/employees',
          component: Employee
        },
        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/discounts',
          component: Discounts
        },
        {
          path: '/accounts',
          component: MyAccount,
          name: 'MyAccount'
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      redirect: '/subLogin',
      children: [
        {
          path: '/subLogin',
          name: 'subLogin',
          component: SubLogin
        },
        {
          path: '/registry',
          name: 'registry',
          component: Registry
        },
        {
          path: '/findPwd',
          name: 'findPwd',
          component: FindPwd
        },
        {
          path: '/registrySuccess',
          name: 'RegistrySuccess',
          component: RegistrySuccess
        },
        {
          path: '/findPwdSuccess',
          name: 'FindPwdSuccess',
          component: FindPwdSuccess
        }
      ]
    },
    {
      path: '/',
      redirect: '/header'
    },
    {
      path: '/header',
      component: Header,
      redirect: '/depCateGoods',
      children: [
        {
          path: '/specialGoods',
          component: SpecialGoods,
          name: 'specialGoods'
        },
        {
          path: '/depCateGoods',
          component: DepCateGoods
        },
        {
          path: '/goodsList',
          component: GoodsList,
          name: 'goodsList'
        },
        {
          path: '/discountsList',
          component: DiscountsList,
          name: 'discountsList'
        }
      ]
    }
  ]
})
// 挂载路由导航守卫
// to 将要访问的路径
// from 代表从哪里来路径跳转而来
// next 是一个函数 next()表示放行 next('/login')强制跳转
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') return next()
//   const tokenStr = window.sessionStorage.getItem('token')
//   if (!tokenStr) return next('/login')
//   next()
// })
export default router
